package components;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DeletePointDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private HashMap< String, ArrayList<Pair<Double, Double >> > points = new HashMap<String,  ArrayList<Pair<Double, Double >> >();
	private JTextField TF_x;
	private JTextField TF_y;
	private JComboBox<String> values;
	private JLabel lblPointHasBeen;

	
	/**
	 * Create the dialog.
	 */
	public DeletePointDialog() {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			values = new JComboBox();
			values.setBounds(223, 40, 109, 27);
			contentPanel.add(values);
		}
		{
			JLabel lblLinguisticValue = new JLabel("Linguistic Value:");
			lblLinguisticValue.setBounds(94, 46, 119, 21);
			contentPanel.add(lblLinguisticValue);
		}
		{
			JLabel lblX = new JLabel("X:");
			lblX.setBounds(183, 99, 12, 15);
			contentPanel.add(lblX);
		}
		{
			JLabel lblY = new JLabel("Y:");
			lblY.setBounds(183, 144, 12, 15);
			contentPanel.add(lblY);
		}
		
		setX(new JTextField());
		getPointX().setBounds(223, 96, 109, 18);
		contentPanel.add(getPointX());
		getPointX().setColumns(10);
		
		setY(new JTextField());
		getPointY().setBounds(223, 141, 109, 18);
		contentPanel.add(getPointY());
		getPointY().setColumns(10);
		{
			lblPointHasBeen = new JLabel("Point has been added");
			lblPointHasBeen.setVisible(false);
			lblPointHasBeen.setBounds(94, 204, 196, 15);
			contentPanel.add(lblPointHasBeen);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Add");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Double x = Double.parseDouble( TF_x.getText() );
						Double y = Double.parseDouble( TF_y.getText() );
						Pair<Double, Double> pair = new Pair<Double, Double>( x, y);			
						if( !points.containsKey( getSelectedValue() ) ){
							points.put( getSelectedValue(), new ArrayList<Pair<Double, Double>>() );
						}
						points.get( getSelectedValue() ).add(pair);
						lblPointHasBeen.setVisible(true);
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						lblPointHasBeen.setVisible(false);
						TF_x.setText("");
						TF_y.setText("");
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	public void setSelection( ArrayList<String> valueNames ){
		for( String name : valueNames ){
			values.addItem(name);
		}
		values.setSelectedIndex(0);
	}


	public JTextField getPointX() {
		return TF_x;
	}


	public void setX(JTextField x) {
		this.TF_x = x;
	}


	public JTextField getPointY() {
		return TF_y;
	}


	public void setY(JTextField y) {
		this.TF_y = y;
	}
	
	private String getSelectedValue(){
		return (String)values.getSelectedItem();
	}
	
	public HashMap<String, ArrayList<Pair<Double, Double>>> getPoints(){
		return points;
	}
}
